
(function(){
    $("#updatePersonalInfo").attr("action", window.globalHost + '/agent-sys/admin/user/modifyUserInfo.do');
    $.ajaxSetup({
        headers: {
            'Authorization': common.getToken()
        },
        cache: false,
        dataType:'json',
        beforeSend: function(xhr, request) {
            if (!request.data) {
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                // request.contentType = "";
                request.data = 'token=' + common.getToken();
                // 'token:' + common.getToken()
            } else {
                request.data += '&token=' + common.getToken();
            }
            request.data= request.data.replace(/[\r\n]/g, '');
        },
        dataFilter: function(data) {
            /**
             * 过滤返回数据中的换行和回车。
             */
            var __data = "{}";
            if (!!data) {
                __data = data.replace(/[\r\n]/g, '');
            }
            if (-1 !== this.dataTypes.join("").indexOf("json")) {
                var result = JSON.parse(__data);
                var msg = result["res"]["msg"];
                // 如果返回值状态码是101或者102，代表token为空（101）或token验证失败（102），那么就弹窗给予提示，并清空缓存，将页面跳转到登录页
                if (result["code"] == 101 || result["code"] == 102) {
                    layer.alert(msg, function() {
                        localStorage.clear();
                        top.parent.location.href = './login.html'
                    });
                }
            }
            return __data;
        }
    });

    window.formObject = $("form");
    // var data = common.getLocalData(common.agentSysCacheWord);
    // var userInfo = data.user;
    var userInfo=common.getLocalData(common.agentSysCacheWord).user;
    /**
     * 动态获取用户个人资料信息并初始化input页面，可修改保存  用户电话号码  和  邮箱
     * */
    $.post(window.globalHost + common.userInfoPath, {id:userInfo.id}).success(function(result) {
        var detailInfo = common.transformData(result);
        common.initInputByName(detailInfo);
        $('#roleName').text(detailInfo.roleName);
        $('#departName').text(detailInfo.departName);
        $('#jobNumber').text(detailInfo.jobNumber);
        $('#userName').text(detailInfo.userName);
        $('#loginName').text(detailInfo.loginName);

        var headerPhotoGallery=$("#header-photo").gallery({
            readOnly:false,
            title:"上传用户头像",
            url:window.globalHost +common.uploadImagePort,
            paramName:"uploadData",
            fileFolder:{folder:"user",id:userInfo.id,fileName:"sys_user_"+new Date().getTime()},
            returnPathName:"filePath",
            maxFiles:1,
            filePaths:detailInfo.photo?[detailInfo.photo]:[],
            uploadCallback:function () {
                var path=headerPhotoGallery.getFilePaths();
                $("#photo").val(!!path.length?path.join(","):"")
            },
            deleteCallback:function () {
                var path=headerPhotoGallery.getFilePaths();
                $("#photo").val(!!path.length?path.join(","):"");
            }
        });
    });
}).call(this);